function changeParams(query){
    let arr = []
    for(let key in query){
        arr.push(key + '=' + query[key])
    }
    return arr.join('&')
}
        
function myAjax(params){
    const p = new Promise((resolve, reject) => {
        //创建一个xhr对象
        const xhr = new XMLHttpRequest()
        //设置返回值的格式
        xhr.responseType = 'json'
        //处理参数
        const query = changeParams(params.data)
        //判断是get还是post
        if (params.method.toUpperCase() === 'GET') {
          xhr.open('GET', query ? params.url + '?' + query : params.url)
          xhr.send()
        } else if (params.method.toUpperCase() === 'POST') {
          xhr.open('POST', params.url)
          xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
          xhr.send(query)
        }
        //监听
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status === 200) {
              resolve(xhr.response)
            } else {
              reject(xhr.status)
            }
          }
        }
      })
    
      return p

}